/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.activity.home.fragment.me;

import static com.dataoke.shoppingguide.app773026.bean.Constants.MEITUAN_RED_PACKAGE_URL;
import static com.dataoke.shoppingguide.app773026.bean.Constants.MEITUAN_SHOPPING_URL;
import static com.dataoke.shoppingguide.app773026.bean.Constants.MEITUAN_SUPERMARKET_URL;

import android.content.Intent;
import android.provider.MediaStore;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.dataoke.shoppingguide.app773026.R;
import com.dataoke.shoppingguide.app773026.activity.setting.SettingActivity;
import com.dataoke.shoppingguide.app773026.activity.user.collection.UserCollectionActivity;
import com.dataoke.shoppingguide.app773026.activity.webview.BrowserActivity;
import com.dataoke.shoppingguide.app773026.base.fragments.BaseFragment;
import com.dataoke.shoppingguide.app773026.bean.haodanku.HaodankuIndexNavBean;
import com.dataoke.shoppingguide.app773026.share.ShareDialog;
import com.dataoke.shoppingguide.app773026.util.AppUtil;
import com.dataoke.shoppingguide.app773026.util.HaoDanKuUtil;
import com.dataoke.shoppingguide.app773026.util.ImageUtils;
import com.dataoke.shoppingguide.app773026.util.UiUtil;
import com.dataoke.shoppingguide.app773026.widget.CircleImageView;
import com.dataoke.shoppingguide.app773026.widget.SolarSystemView;

import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * 用户个人界面
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class UserInfoFragment extends BaseFragment implements View.OnClickListener,
        EasyPermissions.PermissionCallbacks {

    private ShareDialog mShareDialog;

    @BindView(R.id.iv_logo_setting)
    ImageView mIvLogoSetting;
    @BindView(R.id.iv_logo_zxing)
    ImageView mIvLogoZxing;
    @BindView(R.id.user_info_head_container)
    FrameLayout mFlUserInfoHeadContainer;

    @BindView(R.id.iv_portrait)
    CircleImageView mPortrait;
    @BindView(R.id.user_info_icon_container)
    FrameLayout mFlUserInfoIconContainer;

    @BindView(R.id.tv_nick)
    TextView mTvName;
    @BindView(R.id.tv_avail_score)
    TextView mTextAvailScore;
    @BindView(R.id.tv_active_score)
    TextView mTextActiveScore;
    @BindView(R.id.user_view_solar_system)
    SolarSystemView mSolarSystem;
    @BindView(R.id.rl_show_my_info)
    LinearLayout mRlShowInfo;


    @BindView(R.id.about_line)
    View mAboutLine;


    @BindView(R.id.rl_bz)
    LinearLayout mBz;

    private int mMaxRadius;
    private int mR;
    private float mPx;
    private float mPy;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main_user_home;
    }

    private HaodankuIndexNavBean aliRedBean;
    private HaodankuIndexNavBean elemeRedBean;

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        measureTitleBarHeight();
        aliRedBean = JSON.parseObject("{\"id\":58,\"title\":\"支付宝红包\",\"img\":\"https:\\/\\/img-haodanku-com.cdn.fudaiapp.com\\/0_1634267623_567718\",\"link\":\"133\",\"link_type\":7,\"tag\":\"大红包\",\"terrace\":\"1\"}", HaodankuIndexNavBean.class);
        elemeRedBean = JSON.parseObject("{\"id\":12,\"title\":\"饿了么\",\"img\":\"https:\\/\\/img-haodanku-com.cdn.fudaiapp.com\\/0_1624081206_73798\",\"link\":\"116\",\"link_type\":6,\"tag\":\"\",\"terrace\":\"1\"}", HaodankuIndexNavBean.class);
        initSolar();
    }

    /**
     * init solar view
     */
    private void initSolar() {
        View root = this.mRoot;
        if (root != null) {
            root.post(new Runnable() {
                @Override
                public void run() {

                    if (mRlShowInfo == null) return;
                    int width = mRlShowInfo.getWidth();
                    float rlShowInfoX = mRlShowInfo.getX();

                    int height = mFlUserInfoIconContainer.getHeight();
                    float y1 = mFlUserInfoIconContainer.getY();

                    float x = mPortrait.getX();
                    float y = mPortrait.getY();
                    int portraitWidth = mPortrait.getWidth();

                    mPx = x + +rlShowInfoX + (width >> 1);
                    mPy = y1 + y + (height - y) / 2;
                    mMaxRadius = (int) (mSolarSystem.getHeight() - mPy + 250);
                    mR = (portraitWidth >> 1);

                    updateSolar(mPx, mPy);

                }
            });
        }
    }

    /**
     * update solar
     *
     * @param px float
     * @param py float
     */
    private void updateSolar(float px, float py) {

        SolarSystemView solarSystemView = mSolarSystem;
        Random random = new Random(System.currentTimeMillis());
        int maxRadius = mMaxRadius;
        int r = mR;
        solarSystemView.clear();
        for (int i = 40, radius = r + i; radius <= maxRadius; i = (int) (i * 1.4), radius += i) {
            SolarSystemView.Planet planet = new SolarSystemView.Planet();
            planet.setClockwise(random.nextInt(10) % 2 == 0);
            planet.setAngleRate((random.nextInt(35) + 1) / 1000.f);
            planet.setRadius(radius);
            solarSystemView.addPlanets(planet);

        }
        solarSystemView.setPivotPoint(px, py);
    }

    /**
     *
     */
    private void hideView() {
//        mPortrait.setImageResource(R.mipmap.widget_default_face);
        mTvName.setText(R.string.user_hint_login);
        mTvName.setTextSize(16.0f);
        mTextActiveScore.setVisibility(View.INVISIBLE);
        mTextAvailScore.setVisibility(View.INVISIBLE);
        mAboutLine.setVisibility(View.GONE);
    }

    /**
     * measureTitleBarHeight
     */
    private void measureTitleBarHeight() {
        if (mRlShowInfo != null) {
            mRlShowInfo.setPadding(mRlShowInfo.getLeft(),
                    UiUtil.getStatusBarHeight(getContext()),
                    mRlShowInfo.getRight(), mRlShowInfo.getBottom());
        }
    }

    @SuppressWarnings("deprecation")
    @OnClick({
            R.id.iv_logo_setting, R.id.iv_logo_zxing, R.id.iv_portrait,
            R.id.user_view_solar_system, R.id.rl_favorite,R.id.ll_redPackage, R.id.rl_share, R.id.rl_bz,
            R.id.ll_supermarket,R.id.ll_shopping,R.id.ll_aliRedPackage,R.id.ll_elemeRed
    })
    @Override
    public void onClick(View v) {

        int id = v.getId();

        if (id == R.id.iv_logo_setting) {
            SettingActivity.show(mContext);
        } else {
            switch (id) {
                case R.id.iv_logo_zxing:
                    QRCodeDialog dialog = new QRCodeDialog(getActivity(), R.mipmap.qrcode);
                    dialog.show();
                    break;
                case R.id.iv_portrait:
                    //查看头像 or 更换头像
                    break;
                case R.id.user_view_solar_system:
                    //显示我的资料
                    break;
                case R.id.rl_favorite:
                    UserCollectionActivity.show(getActivity());
                    break;
                case R.id.ll_redPackage:
                    AppUtil.openWxMeituan(mContext, MEITUAN_RED_PACKAGE_URL);
                    break;
                case R.id.ll_supermarket:
                    AppUtil.openWxMeituan(mContext, MEITUAN_SUPERMARKET_URL);
                    break;
                case R.id.ll_shopping:
                    AppUtil.openWxMeituan(mContext, MEITUAN_SHOPPING_URL);
                    break;
                case R.id.ll_aliRedPackage:
                    BrowserActivity.show(mContext, HaoDanKuUtil.getUrl(aliRedBean), aliRedBean.getTitle());
                    break;
                case R.id.ll_elemeRed:
                    BrowserActivity.show(mContext, HaoDanKuUtil.getUrl(elemeRedBean), elemeRedBean.getTitle());
                    break;
                case R.id.rl_share:
                    if (mShareDialog == null) {
                        mShareDialog = new ShareDialog(mContext);
                        String title = "网购达人必备的手机 App，网罗全网低价好物";
                        mShareDialog.setTitle(title);
                        mShareDialog.init(getActivity(), title, "全场九块九 包邮您带走", "https://jkj.mailvor.com/?cid=sQwOpnhd");
                        mShareDialog.setShareApp(true);
                    }
                    mShareDialog.show();
                    break;
                case R.id.rl_bz:
                    AppUtil.openWxBz(getContext());
                    break;
                default:
                    break;
            }
        }
    }
    /**
     * take photo
     */
    private void startTakePhoto() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent,
                ImageUtils.REQUEST_CODE_GETIMAGE_BYCAMERA);
    }
    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        try {
            startTakePhoto();
        } catch (Exception e) {
            Toast.makeText(this.getContext(), R.string.permissions_camera_error, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        Toast.makeText(this.getContext(), R.string.permissions_camera_error, Toast.LENGTH_LONG).show();
    }

}
